package com.ftg.learn.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.learn.mall.dto.pmsProductAttributeParam;
import com.ftg.learn.mall.entity.PmsProductAttribute;
import com.ftg.learn.mall.service.IPmsProductAttributeService;
import com.ftg.learn.util.ResEntity;
import com.ftg.learn.util.ResPageEntity;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
@RestController
@RequestMapping("/mall/pmsProductAttribute")
public class PmsProductAttributeController {

    @Autowired
    IPmsProductAttributeService attributeService;

    /**
     * 根据分类查询规格列表或参数列表
     */
    @GetMapping("/findAllAndPage")
    public ResPageEntity findAllAndPage(@RequestParam int type,@RequestParam int page,@RequestParam int limit){
        Page<pmsProductAttributeParam> p = new Page<>(page,limit);
        p.setRecords(attributeService.findAllAndPage(type,p));
        return new ResPageEntity(p);
    }


    @DeleteMapping("/deleteAttributeCountIds")
    public ResEntity deleteAttributeCountIds(@RequestParam List<Long> ids){
        if ( attributeService.updateAttributeCount(ids)) {
            return new ResEntity(0,"success");
        }
        return new ResEntity(-1,"failure");
    }

    @DeleteMapping("/deleteParamCountIds")
    public ResEntity deleteParamCountIds(@RequestParam List<Long> ids){
        if ( attributeService.updateParamCount(ids)) {
            return new ResEntity(0,"success");
        }
        return new ResEntity(-1,"failure");
    }

    /**
     * 添加商品属性参数表
     */
    @PostMapping("/insertattribute")
    public ResEntity insertattribute(@RequestBody PmsProductAttribute pmsProductAttribute){
        return attributeService.insertattribute(pmsProductAttribute) ? new ResEntity() : new ResEntity(-1,"failure");
    }

    @PostMapping("/insertparam")
    public ResEntity insertparam(@RequestBody PmsProductAttribute pmsProductAttribute){
        return attributeService.insertparam(pmsProductAttribute) ? new ResEntity() : new ResEntity(-1,"failure");
    }
    /**
     * 删除商品属性参数表
     * @param id
     * @return
     */
    @DeleteMapping("/delattribute")
    public ResEntity delattribute(@RequestParam Long id){
        return attributeService.delattribute(id) ? new ResEntity() : new ResEntity(-1,"failure");
    }

    @DeleteMapping("/delparam")
    public ResEntity delparam(@RequestParam Long id){
        return attributeService.delparam(id) ? new ResEntity() : new ResEntity(-1,"failure");
    }


    /**
     * 进阶版批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteAndUpdateCount")
    public ResEntity deleteAndUpdateCount(@RequestParam List<Long> ids){
        return attributeService.deleteAndUpdateCount(ids) ? new ResEntity() : new ResEntity(-1,"failure");
    }

    /**
     * 进阶版添加
     * @param pmsProductAttributeParam
     * @return
     */
    @PostMapping("/insertAndUpdateCount")
    public ResEntity insertAndUpdateCount (@RequestBody pmsProductAttributeParam pmsProductAttributeParam){
        return attributeService.insertAndUpdateCount(pmsProductAttributeParam) ? new ResEntity() : new ResEntity(-1,"failure");
    }

    /**
     * 进阶版删除
     * @param id
     * @return
     */
    @DeleteMapping("/delAndUpdateCount")
    public ResEntity delAndUpdateCount(@RequestParam Long id){
        return attributeService.delAndUpdateCount(id) ? new ResEntity() : new ResEntity(-1,"failure");
    }

    /**
     * 修改
     */
    @PutMapping("/updateattribule")
    public ResEntity updateattribule(@RequestBody PmsProductAttribute pmsProductAttribute){
        return attributeService.updateattribule(pmsProductAttribute) ? new ResEntity() : new ResEntity(-1,"failure");
    }
}
